package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.dto.UnLikesBehaviorDto;
import com.heima.behavior.entity.ApArticle;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.feign.ApArticleController;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-18
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior> implements IApUnlikesBehaviorService {

    @Autowired
    IApBehaviorEntryService entryService;

    @Autowired
    private ApArticleController apArticleFeign;

    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {

        if (dto == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        User user = AppThreadLocalUtil.get();

      //  ApLikesBehavior apLikesBehavior = new ApLikesBehavior();

        ApUnlikesBehavior apLikesBehavior = new ApUnlikesBehavior();

        Integer userId = user.getUserId();

        if (userId != 0) {

            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId,userId.toString());

            query.eq(ApBehaviorEntry::getType,1);

            ApBehaviorEntry one = entryService.getOne(query);

            if(one==null){

                one = new ApBehaviorEntry();

                one.setCreatedTime(new Date());

                one.setType(1);

                one.setEntryId(userId.toString());

                entryService.save(one);
            }
            apLikesBehavior.setEntryId(one.getId());
        }else {
            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId,dto.getEquipmentId());

            //   String equipmentId = com.heima.search.dto.getEquipmentId();

            query.eq(ApBehaviorEntry::getType,0);

            ApBehaviorEntry one = entryService.getOne(query);

            if(one==null){

                one = new ApBehaviorEntry();

                one.setCreatedTime(new Date());

                one.setType(0);

                one.setEntryId(dto.getEquipmentId());

                entryService.save(one);
            }
            apLikesBehavior.setEntryId(one.getId());
        }
        apLikesBehavior.setCreatedTime(new Date());

       // apLikesBehavior.setOperation(com.heima.search.dto.getOperation());
        apLikesBehavior.setType(dto.getType());

        apLikesBehavior.setArticleId(dto.getArticleId());

        this.save(apLikesBehavior);

      /*  ApArticle apArticle = apArticleFeign.selectArticleById(com.heima.search.dto.getArticleId());

        *//*int likes = apArticle.getLikes();

        Integer i = ++likes;

        apArticle.setLikes(i);*//*

        int views = apArticle.getViews();

        Integer integer1 = ++views;

        apArticle.setViews(integer1);

        ResponseResult responseResult = apArticleFeign.selectArticleById(apArticle);

        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {

        return ResponseResult.okResult(apLikesBehavior.getId());
        }
        return null;*/
        return ResponseResult.okResult(apLikesBehavior.getId());
    }
}
